package com.lin.http.helloworld;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
import io.netty.handler.ssl.SslContext;

public class HttpHelloWorldServerInitializer extends ChannelInitializer<SocketChannel> {
	private SslContext sslContext;

	public HttpHelloWorldServerInitializer(SslContext sslContext) {
		this.sslContext = sslContext;
	}

	@Override
	protected void initChannel(SocketChannel ch) {
		ChannelPipeline pipeline = ch.pipeline();
		if (sslContext != null) {
			pipeline.addLast(sslContext.newHandler(ch.alloc()));
		}
		pipeline.addLast(new HttpServerCodec());
		// 发送状态码为100 continue的HttpResponse回应包含expect: 100-continue Header的HttpRequest
		// 只能用于没有安装HttpObjectAggregator的应用程序
		pipeline.addLast(new HttpServerExpectContinueHandler());
		pipeline.addLast(new HttpHelloWorldServerHandler());
	}
}
